package cn.yunyichina.provider.hquery.thread;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.exception.CallException;
import cn.yunyichina.provider.hquery.dao.CheckBodyDao;
import cn.yunyichina.provider.hquery.dao.CheckBodyDetailDao;
import cn.yunyichina.provider.hquery.entity.CheckBody;
import cn.yunyichina.provider.hquery.entity.CheckBodyDetail;
import cn.yunyichina.provider.hquery.vo.ExaminationVo;
import cn.yunyichina.provider.iface.entity.base.MedicalCardVo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineDetailVoResponse;
import cn.yunyichina.provider.iface.entity.hisiface.examination.ExamineListVoResponse;
import cn.yunyichina.utils.convert.JsonUtils;

/**
 * Created by 15-AK on 2016/12/15.
 */
public class ExamineDetailRunnable  implements Runnable{
	
    private static final Logger logger = LoggerFactory.getLogger(ExamineDetailRunnable.class);
    
    private ExamineDetailVoResponse examineDetailVoResponse;
    
    private ExaminationVo requestVo;
    
    private CheckBodyDao checkBodyDao = SpringContextHandler.getBean(CheckBodyDao.class);
    
    private CheckBodyDetailDao checkBodyDetailDao = SpringContextHandler.getBean(CheckBodyDetailDao.class);
    
    public ExamineDetailRunnable(ExamineDetailVoResponse examineDetailVoResponse,ExaminationVo requestVo) {
        this.examineDetailVoResponse = examineDetailVoResponse;
        this.requestVo = requestVo;
    }

    @Override
    public void run() {

        logger.info("保存检查明细记录开始======examineDetailVoResponse ="+JsonUtils.toJsonString(examineDetailVoResponse));
        
        
        //存检查明细信息
        if (examineDetailVoResponse != null) {
        	CheckBody checkBody = new CheckBody();
        	checkBody.setOrgCode(requestVo.getHospitalCode());
        	checkBody.setCheckId(requestVo.getCheckId());
			List<CheckBody> checkBodyList = checkBodyDao.select(checkBody);
			
        	//数据库不存在检查记录就将记录存进数据库
        	if (checkBodyList != null && checkBodyList.size() == 1) {
        		
        		//查找检查明细信息,不存在就插入
        		CheckBodyDetail CheckBodyDetailPo = new CheckBodyDetail();
            	CheckBodyDetailPo.setCheckBodyId(checkBodyList.get(0).getId());
    			List<CheckBodyDetail> checkBodyDetailList = checkBodyDetailDao.select(CheckBodyDetailPo);
        		
    			if(checkBodyDetailList.size() == 0){
    				CheckBodyDetail checkBodyDetail = new CheckBodyDetail();
            		checkBodyDetail.setId(PKGenerator.generateId());
            		checkBodyDetail.setCheckBodyId(checkBodyList.get(0).getId());
            		checkBodyDetail.setCheckId(checkBodyList.get(0).getCheckId());
            		checkBodyDetail.setCheckType("".equals(checkBodyList.get(0).getCheckType())?null:Short.valueOf(checkBodyList.get(0).getCheckType()));
            		checkBodyDetail.setCheckName(checkBodyList.get(0).getCheckName());
            		checkBodyDetail.setDoctorCode("");
            		checkBodyDetail.setDoctorName("");
            		checkBodyDetail.setReportTime("".equals(checkBodyList.get(0).getReportTime())?null:checkBodyList.get(0).getReportTime());
            		checkBodyDetail.setCheckPart(examineDetailVoResponse.getCheckPart());
            		checkBodyDetail.setCheckMethod(examineDetailVoResponse.getCheckMethod());
            		checkBodyDetail.setCheckPic(checkBodyList.get(0).getFileAddress());
            		checkBodyDetail.setCheckSituation(examineDetailVoResponse.getCheckSituation());
            		checkBodyDetail.setDiagnosticOption(examineDetailVoResponse.getOption());
            		checkBodyDetail.setAdvice(examineDetailVoResponse.getAdvice());
            		checkBodyDetail.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            		
            		//插入检查列表明细接口
            		logger.info("要新增的检查明细=="+JsonUtils.toJsonString(checkBodyDetail));
            		checkBodyDetailDao.insert(checkBodyDetail);
    			}
        	}
        }
    }
}
